<?php
/**
  * Mar 5, 2008
  *
  * Coded by: Ropu
  *
  */
include('config.php');

$id = $_GET['user_id'];
$Hi5AuthToken = $_GET['Hi5AuthToken'];
$network = $_GET['env'];

define('API_KEY', $api_key);
define('HI5_AUTH_TOKEN', $Hi5AuthToken);
define('ID', $id);


//$hi5Auth = new Hi5Auth($api_key, $id, $Hi5AuthToken);
//$Hi5AuthToken = (string) new SimpleXMLElement($hi5Auth->plain('rovagnati@gmail.com', 'i430fx'));


if(!(isset($_SESSION['os']) && $_SESSION['os'] instanceof OpenSocial)) {
//	$skip = false;
	try {
	  $hi5Auth = new Hi5Auth($api_key, $id, $Hi5AuthToken);
	  if(!$hi5Auth->validateSession()) {
	    Log::medium('Invalid Token\n id: ' . $id . "\nToken: ".$Hi5AuthToken);
	    header('HTTP/1.0 403 Invalid Login');
	    die('403 - Invalid Token');
	  }
//	  $hi5 = new Hi5Profile($api_key, $id, $Hi5AuthToken);
//		$user = new SimpleXMLElement($hi5->foaf(), null, null, 'foaf', true);
//		$p = $user->Person;
//		$user = new SimpleXMLElement($hi5->user(), null, null, 'hi5', true);
//		
	//	echo $p->nick;
	//	echo $p->givenName;
	//	echo $p->surName;
	//	echo $p->img->attributes()->resource;
	} catch(Exception $e) {
	  Log::medium('error connecting to rest server' . $e->getTrace());
//	  $skip = true;
	}
  $_SESSION['os'] = new OpenSocial($id, $network);
  $hacker = $_SESSION['os']->getHacker();
//  $hacker = new Hacker();
  
//  $hacker->setHackathon(1);
//  $hacker->setNickname((string)@$p->nick);
//  $hacker->setExtraInfo('City: ' . (string)@$user->city . "\n" . (string)@$user->interests);
//  $hacker->setName((string)@$p->givenName . " " . (string)@$p->surName);
//  $hacker->setEmail((string)@$user->email);
//  $hacker->setPicutre((string)$p->img->attributes()->resource);
//  $hacker->save();

  $_SESSION['Hi5AuthToken'] = $Hi5AuthToken;
  $_SESSION['uid'] = $id;
} else {
  $hacker = $_SESSION['os']->getHacker();
}

if(@$_SESSION['Hi5AuthToken'] != $Hi5AuthToken || @$_SESSION['uid'] != $id) {
    Log::medium('Invalid Session\n id: ' . $id . "\nToken: ".$Hi5AuthToken);
    header('HTTP/1.0 403 Invalid Login');
    die('403 - Invalid Token');
}


$_SESSION['hacker'] = $hacker;
